<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/9/009
 * Time: 22:15
 */

namespace app\common\service;
use think\Db;

/**
 * 抽奖服务类
 * Class DrawService
 * @package app\common\service
 */
class DrawService
{



    /**
     * 奖次缓存前缀
     * @var string
     */
    protected static $jackpotPrefix = 'jackpot_';

    /**
     * 第二套奖池后缀
     * @var string
     */
    protected static $jackpotSuffix = 'sec';

    /**
     * 奖池配置表
     * @var string
     */
    public static $table = 'draw';

    /**
     * 奖池生成中标识
     * @var string
     */
    protected static $mark = 'mark_';

    /**
     * 生成奖池
     * @param string $type
     * @return bool
     */
    public static function createJackpot(string $type) :bool
    {
        
        $redis     = redisConnect();
        $firstLen  = $redis->lLen(self::$jackpotPrefix.$type);
//        $secondLen = $redis->lLen(self::$jackpotPrefix.$type.self::$jackpotSuffix);

//        && $secondLen == 0
        if ($firstLen == 0 && !cache(self::$mark)){

            //奖池生成标识
            cache(self::$mark,true,10);

            //奖池已被抽空并且没有在生成新奖池   生成新奖池
            $map['status'] = 1;
            $map['type']   = 1;
            $array = Db::name(self::$table)->where($map)->select();
            foreach ($array as $k => $v){
                //第一套奖池
                if ($v[$type] != 0){
                    for ($i = 0;$i < $v[$type];$i++){
                        $first[] = $v['id'];
                    }
                }
                if ($v[$type.self::$jackpotSuffix] != 0){
                    //第二套奖池
                    for ($i = 0;$i < $v[$type.self::$jackpotSuffix];$i++){
                        $second[] = $v['id'];
                    }
                }


            }

            //打乱奖池
            shuffle($first);
            shuffle($second);


            //奖次入队
            foreach ($first as $value){
                $redis->rpush(self::$jackpotPrefix.$type, $value);
            }
            foreach ($second as $value){
                $redis->rpush(self::$jackpotPrefix.$type.self::$jackpotSuffix, $value);
            }
            cache(self::$mark,null);
            return true;

        }

        return false;

    }


    public static function draw1(string $type)
    {
        $redis     = redisConnect();
        $firstLen  = $redis->lLen(self::$jackpotPrefix.$type);

        if ($firstLen > 0) {
            $pot = self::$jackpotPrefix . $type;
        }else{
            $result = self::createJackpot($type);
            if ($result === false){
                api_return(0,'服务器繁忙,请稍后重试');
            }
            return self::draw1($type);
        }
        return $redis->lpop($pot);
    }


    /**
     * 抽奖
     */
    public static function draw(string $type)
    {
        $redis     = redisConnect();
        $firstLen  = $redis->lLen(self::$jackpotPrefix.$type);

        if ($firstLen > 0){
            $pot = self::$jackpotPrefix.$type;
        }else{
            $secondLen = $redis->lLen(self::$jackpotPrefix.$type.self::$jackpotSuffix);
            if ($secondLen > 0){
                $pot = self::$jackpotPrefix.$type.self::$jackpotSuffix;
            }else{
                $result = self::createJackpot($type);
                if ($result === false){
                    api_return(0,'服务器繁忙,请稍后重试');
                }
                return self::draw($type);
            }
        }
        return $redis->lpop($pot);
    }

    /**
     * 查看奖池
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public static function show(string $type)
    {
        $redis  = redisConnect();
        $first  = $redis->lRange(self::$jackpotPrefix.$type,0,-1);
        $second = $redis->lRange(self::$jackpotPrefix.$type.self::$jackpotSuffix,0,-1);
        return ['first'=>$first,'second'=>$second];
    }

    /**
     * 删除奖池
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public static function delete(string $type)
    {
        $redis  = redisConnect();
        $redis->del(self::$jackpotPrefix.$type);
        $redis->del(self::$jackpotPrefix.$type.self::$jackpotSuffix);
    }



}